﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Engine.Helpers.*;
    import Classes.*;
    import Classes.sim.*;

    public class ActionNavigateBeeline extends NPCAction {

        public static const EASE_NONE:String = "none";
        public static const EASE_QUAD_IN:String = "quadIn";
        public static const EASE_QUAD_OUT:String = "quadOut";

        protected static var m_turnStateToDirection:Array = [2, 11, 4, 10, 3, 9, 7, 8, 0, 15, 6, 14, 1, 13, 5, 12];

        protected var m_path:Array;
        protected var m_source:Vector3;
        protected var m_target:Vector3;
        protected var m_velocityScale:Number = 1;

        public function ActionNavigateBeeline(_arg1:NPC, _arg2:Vector3, _arg3:Vector3=null){
            super(_arg1);
            this.m_source = _arg3;
            this.m_target = _arg2;
        }
        public static function getAbsoluteMovementDirection(_arg1:Vector3):int{
            var _local2:Number = (((Math.atan2(_arg1.y, _arg1.x) * (180 / Math.PI)) + 360) % 360);
            var _local3:Number = (360 / m_turnStateToDirection.length);
            var _local4:int = int((_local2 / _local3));
            return (m_turnStateToDirection[_local4]);
        }
        public static function easeInQuad(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number):Number{
            _arg1 = (_arg1 / _arg2);
            return ((((_arg4 * _arg1) * _arg1) + _arg3));
        }
        public static function easeOutQuad(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number):Number{
            _arg1 = (_arg1 / _arg2);
            return ((((-(_arg4) * _arg1) * (_arg1 - 2)) + _arg3));
        }

        public function setVelocityScale(_arg1:Number):ActionNavigateBeeline{
            this.m_velocityScale = _arg1;
            return (this);
        }
        protected function get navigationVelocity():Number{
            return ((m_npc.velocityWalk * this.m_velocityScale));
        }
        override public function update(_arg1:Number):void{
            var _local7:PathElement;
            var _local8:Vector3;
            var _local9:Vector3;
            if (_arg1 > 0.5){
                _arg1 = 0.5;
            };
            if (((!(this.m_path)) || ((this.m_path.length == 0)))){
                m_npc.getStateMachine().popState();
                return;
            };
            if (Global.world.citySim.roadManager.isShowingOverlay){
                m_npc.m_navDbgPath = this.m_path;
            } else {
                m_npc.m_navDbgPath = null;
            };
            var _local2:Number = (_arg1 * this.navigationVelocity);
            var _local3:Boolean;
            do  {
                _local7 = this.m_path[0];
                _local8 = _local7.offsetPosition;
                _local9 = _local8.subtract(m_npc.getPosition());
                _local3 = (_local9.length() > _local2);
                if (!_local3){
                    m_npc.setPosition(_local8.x, _local8.y, _local8.z);
                    _local2 = (_local2 - _local9.length());
                    this.m_path.shift();
                };
                if (this.m_path.length == 0){
                    _local3 = true;
                };
            } while (!(_local3));
            var _local4:Vector3 = ((_local9.length())>0) ? _local9.normalize().scale(_local2) : new Vector3(0, 0, 0);
            var _local5:Vector3 = m_npc.getPosition().add(_local4);
            m_npc.setPosition(_local5.x, _local5.y, _local5.z);
            var _local6:int = ActionNavigate.getMovementDirection(_local9);
            if ((m_npc is Helicopter)){
                _local6 = getAbsoluteMovementDirection(_local9);
            };
            if (_local6 != m_npc.getDirection()){
                if ((m_npc is Vehicle)){
                    (m_npc as Vehicle).startTurnAnimation(_local6);
                } else {
                    m_npc.setDirection(_local6);
                    m_npc.setState(m_npc.getState());
                };
            };
            m_npc.conditionallyReattach();
        }
        protected function updatePath():void{
            if (this.m_source == null){
                this.m_source = m_npc.getPosition();
            };
            m_npc.setPosition(this.m_source.x, this.m_source.y, this.m_source.z);
            this.m_path = [new PathElement(this.m_target, this.m_target)];
            this.debugPrintPath();
        }
        protected function debugPrintPath():void{
        }
        override public function enter():void{
            super.enter();
            this.updatePath();
        }
        override public function reenter():void{
            super.reenter();
            this.updatePath();
        }

    }
}//package Classes.actions 
